<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入客服名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <!--  <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
v-for="dict in getDictDatas(DICT_TYPE.USER_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <!-- <el-form-item label="组别" prop="group">
        <el-input v-model="queryParams.group" placeholder="请输入组别" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="店铺" prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <el-form-item label="app账号" prop="appAccountNumber" >
        <el-input v-model="queryParams.appAccountNumber" placeholder="请输入app账号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <!--    <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
v-for="dict in getDictDatas(DICT_TYPE.COMMON_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <el-form-item label="排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTimes">
        <el-date-picker
v-model="queryParams.createTimes" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>



    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="openFormst()">+新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['table:kefu:export']">导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <!-- <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.USER_TYPE" :value="scope.row.type" />
        </template>
</el-table-column> -->
      <!-- <el-table-column label="组别" align="center" prop="group" /> -->
      <el-table-column label="头像" align="center" prop="faceImg" >


      <template #default="{ row }">
          <div class="flex">
            <el-image
v-if="row.faceImg&&row.faceImg!=undefined"
              fit="cover"
              :src="getImgOne( row.faceImg)"
              class="flex-none w-50px h-50px"
              @click="imagePreview(row.faceImg)"
            />
            <div class="ml-4 overflow-hidden">
              <el-tooltip effect="dark" :content="row.name" placement="top">
                <div>
                  {{ row.name }}
                </div>
              </el-tooltip>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="店铺" align="center" prop="shopId" /> -->
      <!-- <el-table-column label="app用户编号" align="center" prop="appAccountNumber" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <!-- <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" /> -->
          {{zhuangtais(scope.row.status)}}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ timestampToDatetime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openFormst(scope.row.id)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/> -->
    <!-- 对话框(添加 / 修改) -->

    <KefuForm
:id="goId" v-if="showAdd" :class="goId && goId != undefined ? 'showsa' : 'showsb'" @quxiaos="quxiaos"
      @success="getList" />

  </div>
</template>

<script>
import Comm from '@/utils/Comm'
import * as KefuApi from '@/api/kefu';
import KefuForm from './aa.vue';
import { ElMessageBox } from 'element-plus';
const baseImgUrl = ref(import.meta.env.VITE_APP_DEFAULT_IMGURL)

// import SkuForm from '@/views/mall/product/spu/form/InfoForm.vue'
export default {
  name: "Kefu",
  components: {
    KefuForm
  },
  data() {
    return {
      goId: "",
      showAdd: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客服列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 200,
        name: null,
        types: null,
        groupName: null,
        faceImg: null,
        shopId: null,
        appAccountNumber: null,
        status: null,
        sort: null,
        createTimes: [],
      },
    };
  },

  watch: {
    '$route.params.id'(newValue, oldValue) {
      // 这里 newValue 是新的路由参数值，oldValue 是旧的路由参数值（Vue Router 3.x）
      // Vue Router 4.x 则直接获得 newValue
      // 在这里调用你想要在子页面切换时触发的方法
      console.log("<<<<<<<<<<<<<<<<<watchs", newValue, oldValue)
      if (newValue !== oldValue) {
        const userId = this.$route.params.id;
      }
    }
  },

  created() {
    this.getList();
  },
  methods: {

    getImgOne  (url) {
  if(url){
    if(url.indexOf("http")==-1 &&  url.indexOf(baseImgUrl.value)==-1){
      url= baseImgUrl.value+url; 
    }
  }
  return url;
},
    zhuangtais(status){
      switch(status){
         case -1:
          return "已拒绝";
          case 0:
          return "未确定"; 
          case 1:
          return "已同意";   
          case 2:
          return "已停用";   
      }
    },
    timestampToDatetime(timestamp) {
      const dateObj = new Date(timestamp);

      const year = dateObj.getFullYear();
      const month = dateObj.getMonth() + 1;
      const day = dateObj.getDate();
      const hours = dateObj.getHours();
      const minutes = dateObj.getMinutes();
      const seconds = dateObj.getSeconds();

      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    },
    quxiaos() {
      this.showAdd = false;
    },
    /** 查询列表 */
    async getList() {
      console.log("》》》》》》M<<>>>>>>>res",this.queryParams)
    
      try {
        this.queryParams.pageNo=1;
        this.loading = true;
        const res = await KefuApi.getKefuPage(this.queryParams);
        console.log(res, "M<<>>>>>>>res")
        // for (const key in res.list) {
        //   res.list[key].faceImg=Comm.getImgOne(  res.list[key].faceImg)
        // }
        this.list = res.list;
        this.total = res.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openFormst(id) {
      this.showAdd = true;
      this.goId = id;
      // this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
       handleDelete(row) {
      
         ElMessageBox.confirm('是否确认删除客服编号为"' +row.id + '"的数据项?', '提示', {
        confirmButtonText: '确 认',
        cancelButtonText: '取 消'
      })
         .then(() => {
        //   console.log(":::::::::::::>>>>>>>>>>>>")
        
          let uis=  KefuApi.deleteKefu(row.id )
        //       console.log("::::::res:::"+res+"::::>>>>>res>>>>>>>");
        //       if(res){
        //         ElMessageBox.success("删除成功");
        //       }
        //       this.getList();
        //    });
           console.log(uis+"uisuis:::::::::::::>>>>>>>>>>>>")
             uis.then((res) => {
              console.log("::::::res:::"+res+"::::>>>>>res>>>>>>>");
              // if(res){
              //   ElMessageBox.success("删除成功");
              // }
              this.getList();
           });
       
          })
      // const id = row.id;
      // await this.$modal.confirm('是否确认删除客服编号为"' + id + '"的数据项?')
      // try {
      //  await KefuApi.deleteKefu(id);
      //  await this.getList();
      //  this.$modal.msgSuccess("删除成功");
      // } catch {}
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有客服数据项?');
      try {
        this.exportLoading = true;
        const res = await KefuApi.exportKefuExcel(this.queryParams);
        this.$download.excel(res.data, '客服.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    }
  }
}
</script>
<style scoped>
.showsa {
  z-index: 99999;
  position: absolute;
  left: 10%;
  bottom: 2%;
  width: 80%;
  background-color: white;
  height: 90vh;
  top: 4%;
  overflow: auto;
}

.showsb {
  z-index: 99999;
  position: absolute;
  left: 10%;
  top: 5vh;
  width: 80%;
  background-color: white;
  height: 70vh;
  overflow: auto;
  padding-top: 5vh;
}
</style>